$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/manage/payRecord/list',
        datatype: "json",
        colModel: [
			{ label: '订单ID', name: 'orderId', index: 'orderId', width: 50,hidden:true ,key:true},
			{ label: '用户名', name: 'username', index: 'username', width: 50 },
            { label: '手机号', name: 'phone', index: 'phone', width: 50 },
            { label: '订单编号', name: 'orderNum', index: 'orderNum', width: 80 },

			// { label: 'app关键字', name: 'appKey', index: 'appKey', width: 80 },
            { label: '交易银行', name: 'payBank', index: 'payBank', width: 80 },
            { label: '交易卡', name: 'payCardNo', index: 'payCardNo', width: 80 },
			{ label: '订单金额', name: 'amountTotal', index: 'amountTotal', width: 50 },
            { label: '服务费', name: 'serviceCost', index: 'serviceCost', width: 50 },
            { label: '网关成本', name: 'gatewayCost', index: 'gatewayCost', width: 50 },
            { label: '利润', name: 'profit', index: 'profit', width: 50 },
            { label: '使用通道', name: 'accessName', index: 'accessName', width: 50 },
            { label: '通道名称', name: 'accessFormerly', index: 'accessFormerly', width: 50 },
            { label: '出款金额', name: 'realAmount', index: 'realAmount', width: 50 },
            { label: '结算银行', name: 'collecBank', index: 'collecBank', width: 50 },
            { label: '结算卡号', name: 'cashCardNo', index: 'cashCardNo', width: 50 },
			{ label: '订单状态', name: 'status', index: 'status', width: 50, formatter: function(value, options, row){
				var item = '';
				switch (value){
					case -1:item='<span class="label label-danger">已取消</span>';break;
                    case 1:item='<span class="label label-danger">未付款</span>';break;
                    case 2:item='<span class="label label-danger">已取消</span>';break;
                    case 3:item='<span class="label label-danger">已退款</span>';break;
                    case 4:item='<span class="label label-danger">处理中</span>';break;
                    // case 5:item='<span class="label label-danger">已取消</span>';break;
                    case 6:item='<span class="label label-danger">提现失败</span>';break;
                    case 7:item='<span class="label label-success">支付成功</span>';break;
                    case 8:item='<span class="label label-success">缴费成功</span>';break;
                    case 9:item='<span class="label label-success">奖励成功</span>';break;
				}
				return item;
			}},

            { label: '刷卡时间', name: 'createDate', index: 'createDate', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条\

        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $("#createDate").flatpickr({
        dateFormat:'Y-m-d H:i:S',
        "locale":"zh"
    });
    $("#endDate").flatpickr({
        dateFormat:'Y-m-d H:i:S',
        "locale":"zh"
    });
    vm.loadAccess();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		showInfo:false,
        showSummary:false,
        summaryData:{"refundCount":{"orderCount":0,"amountTotalCount":0,"serviceCostCount":0,"gatewayCostCount":0,"profitCount":0},"paySuccessCount":{"orderCount":0,"amountTotalCount":0,"serviceCostCount":0,"gatewayCostCount":0,"profitCount":0},"failedCount":{"orderCount":0,"amountTotalCount":0,"serviceCostCount":0,"gatewayCostCount":0,"profitCount":0},"doingCount":{"orderCount":0,"amountTotalCount":0,"serviceCostCount":0,"gatewayCostCount":0,"profitCount":0},"rewaaedCount":{"orderCount":0,"amountTotalCount":0,"serviceCostCount":0,"gatewayCostCount":0,"profitCount":0},"vipSuccessCount":{"orderCount":0,"amountTotalCount":0,"serviceCostCount":0,"gatewayCostCount":0,"profitCount":0},"refundingCount":{"orderCount":0,"amountTotalCount":0,"serviceCostCount":0,"gatewayCostCount":0,"profitCount":0},"noPayCount":{"orderCount":0,"amountTotalCount":0,"serviceCostCount":0,"gatewayCostCount":0,"profitCount":0},"depositSuccessCount":{"orderCount":0,"amountTotalCount":0,"serviceCostCount":0,"gatewayCostCount":0,"profitCount":0}},
		title: null,
        accesses:[],
        record:{"order":{},"customer":{}}
	},
	methods: {
		query: function () {
		    var searchData = vm.searchData();
            $("#jqGrid").jqGrid('setGridParam',{
                datatype:'json',
                postData:{'searchStr':''+JSON.stringify(searchData)+''},
                page:1
            }).trigger("reloadGrid");
		},
        loadAccess:function () {
            $.getJSON("/platform/manage/payConfig/getAccess",null,function (r) {
                if (r.code == 200){
                    vm.accesses = r.result;
                }else
                    alert(r.message);
            })
        },
		getInfo: function(){
            var ids = getSelectedRows();
            if (ids.length > 1){
                alert('不支持批量操作');
                return;
            }else if(ids.length == 0){
                alert('请选择一条数据');
                return;
            }
            vm.showList = false;
            vm.showInfo = true;
            vm.showSummary = false;
			$.get("/platform/manage/payRecord/info/"+ids[0], function(r){
				if (r.code != 200){
				    alert(r.msg);
                }else {
                    vm.record = r.result;
                }
            });
		},
        download:function () {
		    var searchData = vm.searchData();
            var urlStr = "/platform/manage/payRecord/download?t="+Date.parse(new Date()) + "&searchStr="+JSON.stringify(searchData);
            window.open(encodeURI(urlStr),"_blank");
        },
        summary:function () {
            vm.showList = false;
            vm.showInfo = false;
            vm.showSummary = true;
            $.ajax({
                url:"/platform/manage/payRecord/summary",
                dataType:"JSON",
                success:function (r) {
                    if (r.code == 200){
                        vm.summaryData = r.result;
                    }else {
                        alert(r.message);
                    }
                },
                error:function (e) {
                    alert("服务器繁忙");
                }
            })
        },
        reload: function (event) {
            vm.showList = true;
            vm.showInfo = false;
            vm.showSummary = false;
            $('#createDate').val("");
            $('#endDate').val("");
            $('#accessId').val("0");
            $('#phone').val("");
            $('#orderNum').val("");
            $('#orderStatus').val("0");
			$("#jqGrid").jqGrid('setGridParam',{
                page:1
            }).trigger("reloadGrid");
            vm.isUpdate = false;
		},
        searchData:function () {
            var searchData = {};
            var createDate = $('#createDate').val();
            var endDate = $('#endDate').val();
            var accessId = $('#accessId').val();
            var phone = $('#phone').val();
            var orderNum = $('#orderNum').val();
            var orderStatus = $('#orderStatus').val();
            if (createDate != null && createDate != '')
                searchData['createDate'] = createDate;
            if (endDate != null && endDate != '')
                searchData['endDate'] = endDate;
            if (accessId != null && accessId != '' && accessId != '0')
                searchData['accessId'] = accessId;
            if (phone != null && phone != '' && phone.length == 11)
                searchData['phone'] = phone;
            if (orderNum != null && orderNum != '' )
                searchData['orderNum'] = orderNum;
            if (orderStatus != null && orderStatus != '' && orderStatus != 0)
                searchData['status'] = orderStatus;
            return searchData;
        }
	}
});